package liuhao.bank.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;


import liuhao.bank.dao.BankDao;
import liuhao.bank.entity.BankEntity;
import liuhao.bank.service.BankService;
import liuhao.base.SessionUtil;
import liuhao.dto.PageDTO;

public class BankServiceImpl implements BankService  {

	@Override
	public Map<String, Object> findBanks(PageDTO page)  {
		
		Map<String, Object> map=new HashMap<String, Object>();
		SqlSession session = SessionUtil.getSession();
		BankDao dao = session.getMapper(BankDao.class);
		List<BankEntity> listAccounts;
		try {
		
			listAccounts = dao.findBanks(page);
		//2获得查询的条数
		int acount = dao.queryPageAcount();
		
		//获得分页的总数目
		int pageSide = page.getPageSide();
		//如果不能整除，页数加一，否则等于页数除以分页的数
		int yeshu=acount % pageSide==0?acount/pageSide:acount/pageSide+1;
//		System.out.println(yeshu);
		
		
		map.put("messge", "操作成功");
		map.put("fingWithPage", listAccounts);
		map.put("yeshu", yeshu);
		
		//获得当前的页数
		map.put("page",page);
		
	} catch (Exception e) {
		
		//业务失败时的操作
		map.put("messge", "操作失败");
		//获得当前的页数
		map.put("page",page);
		
		e.printStackTrace();
	}
		return map;
	}

	@Override
	public Map<String, Object> addBank(BankEntity entity)  {
		
		Map<String, Object> map=new HashMap<String, Object>();
		SqlSession session = SessionUtil.getSession();
		BankDao dao = session.getMapper(BankDao.class);
		
		try {
		
			 int addBank = dao.addBank(entity);
		
			
			if(addBank==1){
				
				map.put("code", "200");
			}else{
				map.put("code", "333");
			}
		} catch (Exception e) {
			
			//业务失败时的操作
			map.put("code", "333");
			e.printStackTrace();
		}
		return map;
	}
		

	@Override
	public Map<String, Object> deleteBankByName(BankEntity entity) throws Exception {
		return null;
	}

	@Override
	public Map<String, Object> deleteBank(String[] delId)  {
		
		Map<String, Object> map=new HashMap<String, Object>();
		SqlSession session = SessionUtil.getSession();
		BankDao dao = session.getMapper(BankDao.class);
		
		int counter=0;
		for(int i=0;i<delId.length;i++){
			int did = Integer.parseInt(delId[i]);
			
			
			try {
				int id = dao.deleteBank(did);
				if(id>=1){
					//删除成功返回
					map.put("code","200");
					//成功删除的条数
					counter++;
					map.put("mycounter",counter);
					
				}else{
					
					//删除失败返回
					map.put("code",333);
				}
			} catch (Exception e) {
				map.put("code",333);
				e.printStackTrace();
			}
			
		}
		
		return map;
	}

	@Override
	public Map<String, Object> updateBank(BankEntity entity)  {
		
		Map<String, Object> map=new HashMap<String, Object>();
		SqlSession session = SessionUtil.getSession();
		BankDao dao = session.getMapper(BankDao.class);
		
		try {
			 int addBank = dao.updateBank(entity);
		
			if(addBank==1){
				map.put("code", "200");
			}else{
				map.put("code", "333");
			}
		} catch (Exception e) {
			
			//业务失败时的操作
			map.put("code", "333");
			e.printStackTrace();
		}
		return map;
	}

	@Override
	public Map<String, Object> findBankById(Integer id)  {
		
		Map<String, Object> map=new HashMap<String, Object>();
		SqlSession session = SessionUtil.getSession();
		BankDao dao = session.getMapper(BankDao.class);
		
		try {
			BankEntity bankEntity = dao.findBankById(id);
		
			if( null !=bankEntity){
				map.put("code", "200");
				map.put("bankEntity", bankEntity);
			}else{
				map.put("code", "333");
				map.put("bankEntity", "null");
			}
		} catch (Exception e) {
			
			//业务失败时的操作
			map.put("code", "333");
			map.put("bankEntity", "null");
			e.printStackTrace();
		}
		return map;
	}

}
